//
//  ZFCollectionView.swift
//  mapbox
//
//  Created by fyzq on 2023/11/9.
//

import Foundation
import RxSwift



///布局延时
enum ZFLayoutType {
    case top
    case left
}
enum ZFShowType {
    case image
    case text
}

class ZFCollectionConfig{
    var row:Int = 4;
    var item_height:CGFloat = 50.0;
    var insets:UIEdgeInsets = UIEdgeInsets.zero
    var showType:ZFShowType = ZFShowType.text
    var minimumLineSpacing:CGFloat = 0;///滚动方向的间距
    var minimumInteritemSpacing:CGFloat = 0;///滚动方向的间距
    var item_bg_color:UIColor = UIColor.white
    var radius:CGFloat = 0;
}

class ZFCollectionBaseConfig{
    var layoutType:ZFLayoutType = ZFLayoutType.top
    var space:CGFloat = 5;
    var inset:UIEdgeInsets = UIEdgeInsets.zero
    var backgroundColor:UIColor = UIColor.clear
    var radius:CGFloat = 5;
    var contentFont:UIFont = UIFont.systemFont(ofSize: 14)
    var contentTextColor:UIColor = UIColor.darkGray
}

class ZFCollectionTextConfig : ZFCollectionBaseConfig{
    var titleFont:UIFont = UIFont.iconfont(size: 15)
    var titleTextColor:UIColor = UIColor.darkGray


}

class ZFCollectionImageConfig : ZFCollectionBaseConfig{
    var size:CGSize = CGSize(width: 24, height: 24);
    var contentMode:UIView.ContentMode = UIView.ContentMode.scaleAspectFit
    
}



class ZFCollectionView :ZFBaseView {
    
    private var collectionView:UICollectionView!
    var dataArr:[ZFLayoutModal] = [];
    var config:ZFCollectionConfig = ZFCollectionConfig()
    var textConfig:ZFCollectionTextConfig = ZFCollectionTextConfig()
    var imageConfig:ZFCollectionImageConfig = ZFCollectionImageConfig();
    var didSelected: ((ZFLayoutModal,Int) -> Void)?
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setZFCollectionView();
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    fileprivate func setZFCollectionView(){
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
    
        layout.itemSize = CGSize.init(width: 0, height: 0);
        collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: layout);
        collectionView.delegate = self;
        collectionView.dataSource = self;
        self.addSubview(collectionView);
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        collectionView.register(ZFCollectionLayoutImageCell.self, forCellWithReuseIdentifier: "ZFCollectionLayoutImageCell")
        collectionView.register(ZFCollectionLayoutTextCell.self, forCellWithReuseIdentifier: "ZFCollectionLayoutTextCell")
        collectionView.backgroundColor = .clear
    }
    public func reloadData(){
        collectionView.reloadData()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let inset = collectionView.contentInset;
        let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout;
        

        layout.minimumLineSpacing = config.minimumLineSpacing
        layout.minimumInteritemSpacing = config.minimumInteritemSpacing
        var count = dataArr.count / config.row + 1;
        if dataArr.count % config.row  == 0 {
            count -= 1;
        }
        let item_width = (CGRectGetWidth(frame) - inset.left - inset.right - CGFloat(count - 1) * config.minimumInteritemSpacing ) / CGFloat(config.row);
        layout.itemSize = CGSize.init(width: item_width, height: config.item_height)
        collectionView.contentInset = config.insets;
        collectionView.snp.remakeConstraints { make in
            make.edges.equalToSuperview()
            make.height.equalTo(CGFloat(count) * config.item_height + config.insets.top + config.insets.bottom + CGFloat(count - 1) * config.minimumLineSpacing);
        }        
    }
    
}



extension ZFCollectionView:UICollectionViewDelegate,UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        dataArr.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if config.showType == ZFShowType.text {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ZFCollectionLayoutTextCell", for: indexPath) as! ZFCollectionLayoutTextCell;
            cell.modal = self.dataArr[indexPath.row]
            cell.config = textConfig;
            cell.backgroundColor = config.item_bg_color
            cell.addRadius(radius: config.radius)
            return cell
        }
  
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ZFCollectionLayoutImageCell", for: indexPath) as! ZFCollectionLayoutImageCell;
        cell.config = imageConfig;
        cell.modal = self.dataArr[indexPath.row]
        cell.backgroundColor = config.item_bg_color
        cell.addRadius(radius: config.radius)
        return cell;
    }
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if (didSelected != nil) {
            didSelected!(dataArr[indexPath.row],indexPath.row);
        }
    }
}



class ZFCollectionLayoutImageCell:UICollectionViewCell {
    private var imageView:ZFImageView!
    var modal:ZFLayoutModal!{
        didSet{
            imageView.modal = modal
        }
    }
    var config:ZFCollectionImageConfig!{
        didSet{
            imageView.config = config
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setZFCollectionLayoutImageCell()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func setZFCollectionLayoutImageCell(){
        imageView = ZFImageView()
        contentView.addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
    }
    
}

class ZFImageView : ZFBaseView {
    private var imageView:UIImageView!
    private var contentLabel:UILabel!
    var config:ZFCollectionImageConfig!{
        didSet{
            backgroundColor = config.backgroundColor
            addRadius(radius: config.radius)
            imageView.contentMode = config.contentMode
            contentLabel.textColor = config.contentTextColor
            contentLabel.font = config.contentFont
            
            if config.layoutType == ZFLayoutType.left {
                imageView.snp.remakeConstraints{ make in
                    make.centerY.equalToSuperview()
                    make.left.equalToSuperview().offset(config.inset.left)
                    make.top.equalToSuperview().offset(config.inset.top)
                    make.bottom.equalToSuperview().offset(-config.inset.bottom)
                    make.size.equalTo(config.size)
                }
                contentLabel.snp.remakeConstraints { make in
                    make.centerY.equalTo(imageView.snp.centerY)
                    make.top.greaterThanOrEqualToSuperview().offset(config.inset.top)
                    make.bottom.lessThanOrEqualToSuperview().offset(-config.inset.bottom)
                    make.right.equalToSuperview().offset(-config.inset.right)
                    make.left.equalTo(imageView.snp.right).offset(config.space)
                }
                return;
            }
            imageView.snp.remakeConstraints{ make in
                make.centerX.equalToSuperview()
                make.right.lessThanOrEqualToSuperview().offset(-config.inset.right)
                make.left.greaterThanOrEqualToSuperview().offset(config.inset.left)
                make.top.equalToSuperview().offset(config.inset.top)
                make.size.equalTo(config.size)
            }
            contentLabel.snp.remakeConstraints { make in
                make.centerX.equalTo(imageView.snp.centerX)
                make.right.lessThanOrEqualToSuperview().offset(-config.inset.right)
                make.left.greaterThanOrEqualToSuperview().offset(config.inset.left)
                make.top.equalTo(imageView.snp.bottom).offset(config.space)
                make.bottom.equalToSuperview().offset(-config.inset.bottom)
            }
            
        }
        
    }
    var modal:ZFLayoutModal!{
        didSet{
            imageView.image = UIImage.image(for: modal.imageName)
            contentLabel.text = modal.content
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setZFCollectionItemTextView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func setZFCollectionItemTextView(){
        imageView = UIImageView()
        addSubview(imageView)
        
        contentLabel = UILabel()
        contentLabel.font = UIFont.systemFont(ofSize: 15)
        addSubview(contentLabel)
    }
    
}


///文字
class ZFCollectionLayoutTextCell:UICollectionViewCell {
    private var textView:ZFTextView!
    var modal:ZFLayoutModal!{
        didSet{
            textView.modal = modal
        }
    }
    var config:ZFCollectionTextConfig!{
        didSet{
            textView.config = config
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setZFCollectionLayoutTextCell()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func setZFCollectionLayoutTextCell(){
        textView = ZFTextView()
        contentView.addSubview(textView)
        textView.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
    }
    
}


class ZFTextView :ZFBaseView {
    private var titleLabel:UILabel!
    private var contentLabel:UILabel!
    var config:ZFCollectionTextConfig!{
        didSet{
            titleLabel.font = config.titleFont
            titleLabel.textColor = config.titleTextColor
            contentLabel.font = config.contentFont
            contentLabel.textColor = config.contentTextColor
            
            backgroundColor = config.backgroundColor;
            addRadius(radius: config.radius)
            
            
            if config.layoutType == ZFLayoutType.left {
                titleLabel.snp.remakeConstraints{ make in
                    make.centerY.equalToSuperview()
                    make.left.equalToSuperview().offset(config.inset.left)
                    make.top.equalToSuperview().offset(config.inset.top)
                    make.bottom.equalToSuperview().offset(-config.inset.bottom)
                }
                contentLabel.snp.remakeConstraints { make in
                    make.centerY.equalTo(titleLabel.snp.centerY)
                    make.top.greaterThanOrEqualToSuperview().offset(config.inset.top)
                    make.bottom.lessThanOrEqualToSuperview().offset(-config.inset.bottom)
                    make.right.equalToSuperview().offset(-config.inset.right)
                    make.left.equalTo(titleLabel.snp.right).offset(config.space)
                }
                
                return;
            }
            titleLabel.snp.remakeConstraints{ make in
                make.centerX.equalToSuperview()
                make.right.lessThanOrEqualToSuperview().offset(-config.inset.right)
                make.left.greaterThanOrEqualToSuperview().offset(config.inset.left)
                make.top.equalToSuperview().offset(config.inset.top)
            }
            contentLabel.snp.remakeConstraints { make in
                make.centerX.equalTo(titleLabel.snp.centerX)
                make.right.lessThanOrEqualToSuperview().offset(-config.inset.right)
                make.left.greaterThanOrEqualToSuperview().offset(config.inset.left)
                make.top.equalTo(titleLabel.snp.bottom).offset(config.space)
                make.bottom.equalToSuperview().offset(-config.inset.bottom)
            }
        }
    }
    var modal:ZFLayoutModal!{
        didSet{
            titleLabel.text = modal.title
            contentLabel.text = modal.content
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setZFCollectionItemTextView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func setZFCollectionItemTextView(){
        titleLabel = UILabel()
        titleLabel.font = UIFont.iconfont(size: 30)
        addSubview(titleLabel)
        
        contentLabel = UILabel()
        contentLabel.font = UIFont.systemFont(ofSize: 15)
        addSubview(contentLabel)
    }
    
}
